package Frame.common;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Point;
import java.awt.Toolkit;
import java.util.Iterator;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;

import Frame.librarianFrame.BookManageFrame;
import controller.Controller;
import table.Book;

import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import java.awt.event.ActionListener;

public class FindBookFrame extends JFrame {
	private JTable table;
	private Controller controller;
	private JPanel jpanel;
	private JPanel panel1;
	private JTextField tf1;
	private JTextField tf2;
	private JTextField tf3;
	private JTable bookTable;
	private JScrollPane tablePanel;
	public FindBookFrame(Controller controller) {
		this.controller = controller;
		//controller.setBookManageFrame(this);
		jpanel = new JPanel();
		jpanel.setBackground(Color.WHITE);
		jpanel.setLayout(null); 
		jpanel.add(getTablePanel(controller.getLibrarianService().find()));
		jpanel.add(panel1());
		jpanel.setSize(1920,994);

	}
	public JPanel panel1() {
		panel1 = new JPanel();
		panel1.setBackground(Color.WHITE);
		panel1.setBounds(0, 0, 1920, 295);
		panel1.setLayout(null);

		JLabel lb1 = new JLabel("索书号:");
		lb1.setBounds(236, 113, 64, 27);
		lb1.setFont(new Font("华文楷体", Font.PLAIN, 20));
		lb1.addMouseListener(controller);
		lb1.setName("bookBorrow");
		panel1.add(lb1);

		JLabel lb2 = new JLabel("书名:");
		lb2.setBounds(831, 113, 44, 27);
		lb2.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel1.add(lb2);

		JLabel lb3 = new JLabel("作者:");
		lb3.setBounds(1405, 113, 44, 27);
		lb3.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel1.add(lb3);
 
		tf1 = new JTextField();
		tf1.setBounds(314, 111, 171, 31);
		tf1.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel1.add(tf1);
		tf1.setColumns(10);

		tf2 = new JTextField();
		tf2.setBounds(889, 111, 165, 31);
		tf2.setColumns(10);
		tf2.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel1.add(tf2);

		tf3 = new JTextField();
		tf3.setBounds(1461, 111, 165, 31);
		tf3.setColumns(10);
		tf3.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel1.add(tf3);

		JButton bt8 = new JButton("查询");
		bt8.setBounds(907, 191, 119, 35);
		bt8.setIcon(new ImageIcon(BookManageFrame.class.getResource("/logo/搜索.png")));
		bt8.setFont(new Font("华文楷体", Font.PLAIN, 20));
		bt8.addActionListener(controller);
		bt8.setActionCommand("findBookByUser");
		bt8.setContentAreaFilled(false);// 将按钮设置为透明
		panel1.add(bt8);
//		contentPane.add(panel3);
		return panel1;
	}
	public JScrollPane getTablePanel(List<Book> bookList) {
		Object dataName[] = { "书名", "作者", "出版社", "索书号", "数量", "价格", "ISBN" };
		Object data[][] = new Object[bookList.size()][7];
		Iterator<Book> iter = bookList.iterator();
		int i = 0; 
		while (iter.hasNext()) {
			Book bk = iter.next();
			data[i][0] = bk.getBookName();
			data[i][1] = bk.getBookWriter();
			data[i][2] = bk.getBookPublishingHouse();
			data[i][3] = bk.getBookCallNumber();
			data[i][4] = bk.getBookNumber();
			data[i][5] = bk.getBookPrice(); 
			data[i][6] = bk.getBookIsbn();
			i++;
		}
		bookTable = new JTable(data, dataName) {
			@Override
		    public Component prepareRenderer(TableCellRenderer renderer, int row, int column){//设置鼠标光标划过时的颜色改变
	        Component comp = super.prepareRenderer(renderer,row,column); 
		        Point p = getMousePosition();
		    	
		        if(p!=null){
		            int rowUnderMouse = rowAtPoint(p);
		            if(rowUnderMouse == row){
		                  comp.setBackground(new Color(223,240,216));
		           }
//		            else{
//		                  comp.setBackground(DefaultLookup.getColor(this,ui,"Table.alternateRowColor"));
//		           }
		        }		        
		        return comp;
		    }
			private static final long serialVersionUID = 1L;

			public boolean isCellEditable(int row, int column) {
				return false;
			};// 将表设置成无法修改
		};
		bookTable.setSelectionBackground(new Color(223,240,216));//设置选中的行的颜色
		TableBackGround.setColumnColor(bookTable);//设置表的交替行数的颜色 
		bookTable.addMouseMotionListener(controller.getMyTableMouseMotion());
		bookTable.getTableHeader().setFont(new Font("华文楷体", Font.PLAIN, 20)); // 表头字体
		bookTable.setFont(new Font("华文楷体", Font.PLAIN, 21)); // 表体字体
		bookTable.getTableHeader().setBackground(new Color(184,207,240));//设置表头的颜色
		bookTable.getTableHeader().setPreferredSize(new Dimension(15, 35)); // 表头高度 先后为宽 高
		DefaultTableCellRenderer cr = new DefaultTableCellRenderer();// 将表体的内容设置为居中显示
		bookTable.setDefaultRenderer(Object.class, cr);
		bookTable.setRowHeight(bookTable.getRowHeight() + 15); // 表体高度
		bookTable.setRowMargin(5);// 设置相邻行中的单元格之间的空白空间量
		bookTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // 表格只能选择一行
		bookTable.repaint(); 
		tablePanel = new JScrollPane(bookTable);
		tablePanel.getViewport().setBackground(Color.WHITE);//将JScrollPane设置成白色
		tablePanel.setBounds(0, 294, 1920, 581);
		// contentPane.add(tablePanel);
		// bookTable.setLocation(0, 210);
		// bookTable.setSize(1935, 528);
		// bookTable.setVisible(false);
		return tablePanel;
	}

	public void showSearchResult(List<Book> result) {
		jpanel.remove(tablePanel);
		jpanel.add(getTablePanel(result));
		jpanel.validate();
		JOptionPane.showMessageDialog(this, "重新加载数据成功！");
	}

	public void refresh() {
		jpanel.remove(tablePanel);
		jpanel.add(getTablePanel(controller.getLibrarianService().find()));
		jpanel.validate();
		JOptionPane.showMessageDialog(this, "重新加载数据成功！");
	}
	public JPanel getJpanel() {
		return jpanel;
	}
	public void setJpanel(JPanel jpanel) {
		this.jpanel = jpanel;
	}
	public JTextField getTf1() {
		return tf1;
	}
	public void setTf1(JTextField tf1) {
		this.tf1 = tf1;
	}
	public JTextField getTf2() {
		return tf2;
	}
	public void setTf2(JTextField tf2) {
		this.tf2 = tf2;
	}
	public JTextField getTf3() {
		return tf3;
	}
	public void setTf3(JTextField tf3) {
		this.tf3 = tf3;
	}
	
}
